﻿/************************************************************************

 This file is part of "10 Second Tim".

 "10 Second Tim" is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 "10 Second Tim" is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with "10 Second Tim".  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2013 Scott Bevin. all rights reserved

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace LDE.Input
{
#if SILVERLIGHT || XBOX || WINDOWS

	public class GamePadInputDevice : IInputDevice
	{
		#region Members

		private int m_padIdx;
		private static int s_deviceTypeId = ( "GamePad" ).GetHashCode( );

		public static int DeviceTypeId { get { return s_deviceTypeId; } }

		private class GamePadButton : InputButton
		{
			public Buttons Button = Buttons.A;
		}

		private List<GamePadButton> m_buttons = new List<GamePadButton>( );

		#endregion Members

		public GamePadInputDevice( int padId )
		{
			m_padIdx = padId;

			foreach ( Buttons button in Enum.GetValues( typeof( Buttons ) ) )
			{
				GamePadButton nb = new GamePadButton( );
				nb.Button = button;
				m_buttons.Add( nb );
			}
		}

		public int GetDeviceTypeId( )
		{
			return s_deviceTypeId;
		}

		public int GetDeviceId( )
		{
			return m_padIdx;
		}

		public InputButton IsPressed( InputTrigger trigger )
		{
			foreach ( Buttons b in trigger.GamepadButtons )
			{
				foreach ( GamePadButton button in m_buttons )
				{
					if ( button.Button == b )
					{
						if ( button.Down && !button.DownLF )
							return button;
					}
				}
			}

			return null;
		}

		public InputButton IsHeld( InputTrigger trigger )
		{
			foreach ( Buttons b in trigger.GamepadButtons )
			{
				foreach ( GamePadButton button in m_buttons )
				{
					if ( button.Button == b )
					{
						if ( button.Down && button.CurrStateTimer >= InputManager.Holdtime )
							return button;
					}
				}
			}

			return null;
		}

		public InputButton IsDown( InputTrigger trigger )
		{
			foreach ( Buttons b in trigger.GamepadButtons )
			{
				foreach ( GamePadButton button in m_buttons )
				{
					if ( button.Button == b )
					{
						if ( button.Down )
							return button;
					}
				}
			}

			return null;
		}

		public InputButton IsReleased( InputTrigger trigger )
		{
			foreach ( Buttons b in trigger.GamepadButtons )
			{
				foreach ( GamePadButton button in m_buttons )
				{
					if ( button.Button == b )
					{
						if ( !button.Down && button.DownLF )
							return button;
					}
				}
			}

			return null;
		}

		public InputButton IsTapped( InputTrigger trigger )
		{
			foreach ( Buttons b in trigger.GamepadButtons )
			{
				foreach ( GamePadButton button in m_buttons )
				{
					if ( button.Button == b )
					{
						if ( !button.Down && button.DownLF && button.PrevStateTimer < InputManager.TapTime )
							return button;
					}
				}
			}

			return null;
		}

		public void UpdateState( float dt )
		{
			GamePadState state = GamePad.GetState( (PlayerIndex)m_padIdx );

			for ( int i = 0; i < m_buttons.Count; ++i )
			{
				GamePadButton button = m_buttons[ i ];

				button.DownLF = button.Down;
				button.Down = state.IsButtonDown( button.Button );

				if ( button.Down != button.DownLF )
				{
					button.PrevStateTimer = button.CurrStateTimer;
					button.CurrStateTimer = 0.0f;
				}

				button.CurrStateTimer += dt;
			}
		}

		public bool IsConnected( )
		{
			return GamePad.GetState( (PlayerIndex)m_padIdx ).IsConnected;
		}

		public Microsoft.Xna.Framework.Vector2 GetStickVals( )
		{
			return GamePad.GetState( (PlayerIndex)m_padIdx ).ThumbSticks.Left;
		}

		public bool IsPointingDevice( )
		{
			return false;
		}

		public Point GetPointerPos( )
		{
			return Point.Zero;
		}
	}

#endif
}